﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.Data;
using V5_BLL;
using V5_Model;

namespace V5_EShop._Template.Default.Member
{
    public partial class Temporary : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.check_member_login())
            {
                Response.Redirect("/login.aspx");
            }
            if (!this.IsPostBack)
            {
                int id = Input.SetInt(Request["id"]);
                int productID = Input.SetInt(Request["productid"]);
                string action = Input.SetString(Request["Action"]);
                if (action == "del")
                {
                    this.Bind_Delete(id);
                }
                else if (action == "shop")
                {
                    Bind_ShopDelete(id);
                }else
                {
                    if (productID > 0)
                    {
                        Bind_Add(productID);
                    }
                    else
                    {
                        Bind_rpList();
                    }
                }
            }
        }

        private void Bind_rpList()
        {
            int Count=0;
            DataTable dt = new BLLTemporary().GetList(
                Pager.PageSize,
                Pager.CurrentPageIndex,
                " MemberID="+user.ID,
                ref Count).Tables[0];
            this.Pager.UrlRewritePattern = "/member/Temporary.aspx?page={0}";
            this.rpList.DataSource = dt;
            this.rpList.DataBind();
        }

        private void Bind_Add(int ProductID)
        {
            BLLTemporary b_temporay = new BLLTemporary();
            ModelTemporary m_temporay = new ModelTemporary();
           
            if (b_temporay.ExistsByProductID(ProductID))
            {
                //Response.Redirect(HTTPS.ComeUrl);
                Js.AlertAndRedirect("商品已经暂存成功!", HTTPS.ComeUrl);
            }
            else
            {
                ModelProduct model = new BLLProduct().GetModel(ProductID);
                m_temporay.ID = b_temporay.GetMaxId();
                m_temporay.ProductName = model.ProductName;
                m_temporay.ProductPrice = model.ProductMemberPrice;
                m_temporay.MemberID = user.ID;
                m_temporay.ProductID = ProductID;
                m_temporay.AddDateTime = DateTime.Now;
                b_temporay.Add(m_temporay);
                Js.AlertAndRedirect("商品暂存成功!", HTTPS.ComeUrl);
            }
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_rpList();
        }

        private void Bind_Delete(int id)
        {
            ModelTemporary model = new BLLTemporary().GetModel(id);
            if (model.MemberID == user.ID)
            {
                new BLLTemporary().Delete(id);
            }
            Response.Redirect(HTTPS.ComeUrl);
        }

        private void Bind_ShopDelete(int id)
        {
            int productID=0;
            ModelTemporary model = new BLLTemporary().GetModel(id);
            productID = model.ProductID;
            if (model.MemberID == user.ID)
            {
                new BLLTemporary().Delete(id);
            }
            Response.Redirect("/shoppingcart.aspx?pid="+model.ProductID);
        }
    }
}
